package catalina.startup;

import catalina.connection.Connector;
import catalina.container.Container;
import catalina.container.StandardEngine;
import catalina.container.StandardHost;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Catalina implements LifeCycle {


    private StandardServer standardServer;

    @Override
    public void init() {
        parseServerXML();
        standardServer.init();
    }

    @Override
    public void start() {
        standardServer.start();
    }

    @Override
    public void stop() {

    }

    @Override
    public void destroy() {

    }

    /**
     * 加载解析web.xml，初始化Servlet
     */
    private void parseServerXML() {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("conf/server.xml");
        SAXReader saxReader = new SAXReader();

        try {
            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();

            Element serverElement = (Element) rootElement.selectSingleNode("//Server");
            if (serverElement == null) {
                throw new RuntimeException("server.xml--> serverElement is null.");
            }
            this.standardServer = new StandardServer();
            List<Element> serviceNodes = rootElement.selectNodes("//Service");

            List<StandardService> standardServiceList = new ArrayList<>();
            standardServer.setServiceList(standardServiceList);

            for (int i = 0; i < serviceNodes.size(); i++) {
                StandardService standardService = new StandardService();
                standardServiceList.add(standardService);
                Element element = serviceNodes.get(i);
                String name = element.attributeValue("name");
                standardService.setName(name);
                List<Element> connectorNodes = element.selectNodes("//Connector");
                List<Connector> connectors = new ArrayList<>();
                for (Element connectorNode : connectorNodes) {
                    Connector connector = new Connector();
                    Properties properties = new Properties();
                    connector.setProperties(properties);
                    String port = connectorNode.attributeValue("port");
                    properties.put("port", port);
                    connectors.add(connector);
                    connector.setStandardService(standardService);
                }
                standardService.setConnectorList(connectors);

                Element engineElement = (Element) rootElement.selectSingleNode("//Engine");
                String engineName = engineElement.attributeValue("name");
                Container engine = new StandardEngine();
                engine.setName(engineName);
                standardService.setContainer(engine);

                List<Element> hostElements = engineElement.selectNodes("//Host");
                for (Element hostElement : hostElements) {
                    Container host = new StandardHost();
                    String hostName = hostElement.attributeValue("name");
                    host.setName(hostName);
                    host.setParent(engine);
                    Properties properties = new Properties();
                    String appBase = hostElement.attributeValue("appBase");
                    properties.put("appBase",appBase);
                    host.setProperties(properties);
                    engine.addChild(host);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }
}
